package com.guodi.model.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guodi.common.utils.PageData;
import com.guodi.model.entity.IdtMonitorValue;
import com.guodi.model.entity.MapLayer;
import com.guodi.model.entity.MapProject;
import com.guodi.model.mapper.OldMapMapper;
import com.guodi.model.service.IOldMapService;
import com.guodi.model.utils.ZTreeIconSkin;
import com.guodi.model.utils.ZTreeNode;
import com.guodi.system.feign.ISysClient;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @描述： 服务类
 * @作者: 彭辉
 * @日期: 2019-12-04
 */
@Service
public class OldMapServiceImpl extends ServiceImpl<OldMapMapper, IdtMonitorValue> implements IOldMapService {

    @Resource
    private OldMapMapper mapper;

    @Resource
    ISysClient sysClient;

    @Override
    public List<PageData> listProjectByEntity(PageData pd) {
        return mapper.listProjectByEntity(pd);
    }

    @Override
    public List<MapProject> listTreeByProjectIdAndUserId(PageData pd) {
        // 获取所有图层
        List<MapLayer> layerAllList = mapper.getAllLayersByEntity(null);
        // 获取授权专题图层集合
        List<MapProject> authList = mapper.listTreeByProjectIdAndUserId(pd);
        // 专题id
        String projectId = pd.getString("id");

        // 存储返回专题目录树数据集
        List<MapProject> treeList = new ArrayList<>();
        for(MapProject authPd : authList){
            if(projectId.equals(authPd.getParentId())){
                // 递归获取专题目录树
                dataRecursive(layerAllList, authList, authPd, pd.getString("map_extent"));
                treeList.add(authPd);
            }
        }
        return treeList;
    }

    /**
     * @描述：递归获取专题目录/图层树列表
     * @作者：彭辉
     * @时间：2019/1/17 18:42
     */
    private void dataRecursive(List<MapLayer> layerAllList, List<MapProject> authDriList, MapProject currentDirPd, String extent) {
        Integer dirType = Integer.valueOf(currentDirPd.getType());//专题图层类型(1:目录，2：图层，3：组合图层)
        if(dirType > 1 && layerAllList != null && layerAllList.size() > 0){
            // 如果图层，则根据图层id封装图层信息
            for(MapLayer mapLayer : layerAllList){
                if(mapLayer.getId().equals(currentDirPd.getLayerId())){
                    // 存储图层信息pd
                    mapLayer.setInitialExtent(extent);
                    if("layer_type_dt".equals(mapLayer.getLayerType())){
                        mapLayer.setLayerType("6");
                    }else{
                        mapLayer.setLayerType("1");
                    }
                    mapLayer.setFeatureType("1");
                    currentDirPd.setMapLayer(mapLayer);
                    break;
                }
            }
        }
        for(MapProject authDirPd : authDriList){
            if(authDirPd.getParentId().equals(currentDirPd.getId())){
                // 递归获取专题目录树
                dataRecursive(layerAllList, authDriList, authDirPd, extent);
                if(currentDirPd.getChildren() != null){
                    currentDirPd.getChildren().add(authDirPd);
                }else{
                    List<MapProject> temp = new ArrayList<>();
                    temp.add(authDirPd);
                    currentDirPd.setChildren(temp);
                }

            }
        }
    }

    @Override
    public ZTreeNode listTree() {
        // 查询所有数据集
        List<PageData> list = mapper.listDataSet();
        // 存储所有数据集中所有机构名称
        Map<String, String> orgMap = new HashMap<String, String>();
        try {
            for(PageData dsPd : list){
                // 根据机构id获取机构名称
                //String orgName = convertService.toValue(ConvertConst.ORGID2NAME, dsPd.getString("org_id"));
                // 需优化
//                String orgName = sysClient.getOrgName(Long.valueOf(dsPd.getString("org_id"))).getData().getDeptName();
//                orgMap.put(dsPd.getString("org_id"), orgName);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 存储地图资源树数据集
        List<ZTreeNode> treeList = new ArrayList<ZTreeNode>();
        // 添加“地图资源”根节点
        ZTreeNode rootZtreeNode = new ZTreeNode();
        rootZtreeNode.setId("root");
        rootZtreeNode.setName("地图资源");
        rootZtreeNode.setpId("-1");
        rootZtreeNode.setIconSkin(ZTreeIconSkin.ICON_SKIN_ROOT);
        rootZtreeNode.setOpen(true);
        //treeList.add(rootZtreeNode);

        // 根据实体属性查询图层集合
        PageData layerParamPd = new PageData();
        for(String orgId : orgMap.keySet()){
            // 添加“机构”节点
            ZTreeNode orgZtreeNode = new ZTreeNode();
            orgZtreeNode.setId(orgId);
            orgZtreeNode.setName(orgMap.get(orgId));
            orgZtreeNode.setpId(rootZtreeNode.getId());
            orgZtreeNode.setIconSkin(ZTreeIconSkin.ICON_SKIN_ORG);
            orgZtreeNode.setOpen(true);
            //treeList.add(orgZtreeNode);
            // 存储地图资源树数据集
            List<ZTreeNode> mapSetList = new ArrayList<ZTreeNode>();
            for(PageData dsPd : list){
                if(orgId.equals(dsPd.getString("org_id"))){
                    // 添加“数据集”节点
                    ZTreeNode dsZtreeNode = new ZTreeNode();
                    dsZtreeNode.setId(dsPd.getString("id"));
                    dsZtreeNode.setName(dsPd.getString("dataset_name"));
                    dsZtreeNode.setpId(orgZtreeNode.getId());
                    dsZtreeNode.setIconSkin(ZTreeIconSkin.ICON_SKIN_LAYER_DIR);
                    //treeList.add(dsZtreeNode);
                    // 存储图层
                    List<ZTreeNode> mapLayerList = new ArrayList<ZTreeNode>();
                    layerParamPd.put("dataset_id", dsPd.getString("id"));
                    // 根据数据集id查询图层
                    List<PageData> layerList = mapper.listMapLayer(layerParamPd);
                    for(PageData layerPd: layerList){
                        // 添加“图层”节点
                        ZTreeNode ztreeNode = new ZTreeNode();
                        ztreeNode.setId(layerPd.getString("id"));
                        ztreeNode.setName(layerPd.getString("layer_name"));
                        ztreeNode.setpId(dsZtreeNode.getId());
                        ztreeNode.setIconSkin(ZTreeIconSkin.ICON_SKIN_LAYER);

                        try {
                            // 把图层信息存入dataMap中
                            PageData dataPd = new PageData();
                            for(Object key : layerPd.keySet()){
                                dataPd.put(key, layerPd.get(key));
                            }
                            ztreeNode.setPd(dataPd);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        mapLayerList.add(ztreeNode);
                        dsZtreeNode.setChildren(mapLayerList);
                        //treeList.add(ztreeNode);
                    }
                    mapSetList.add(dsZtreeNode);
                }
                orgZtreeNode.setChildren(mapSetList);
            }
            treeList.add(orgZtreeNode);
        }
        rootZtreeNode.setChildren(treeList);
        return rootZtreeNode;
    }
}
